﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Contract.MVC;
using Way.Bus.WMS.Model;
using Way.Contract;
using System.Data;
using Way.Bus.Controllers;
using Way.EAP.DataAccess.Entity;
using Way.Bus.WMS.Controllers;
using Way.Contract.Center;
using Way.EAP.DataAccess.Data;
using Way.EAP.DataAccess.Regulation;
using Way.Contract.Data;
using Way.Bus.WMS.ST.Model;
using Way.Bus.Model;
using Way.Contract.Data.ControllerData;

namespace Way.Bus.WMS.ST.Controllers.OutOrder
{
    public class ST_OutReceiptController : StockOutNoticeController
    {
        //构造方法 controller与model绑定
        public ST_OutReceiptController()
            : base(new ST_OutNotice())
        {
        }
        public ST_OutReceiptController(IModel model) : base(model) { }

        

        protected override string OnControllerName()
        {
            return "签收单";
        }

        //controller 对应的html页面
        protected override string OnGetPath()
        {
            return "Views/ST/OutOrder/OutReceipt.htm";
        }

        protected override string OnGetControllerDescription()
        {
            return "签收单";
        }
        #region 页面设置
        /// <summary>
        /// 禁用审核
        /// </summary>
        public override bool IsReviewedState
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 禁用提交
        /// </summary>
        /// 

        public override bool IsSubmit
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 禁用下推
        /// </summary>
        public override bool IsPushDown
        {
            get
            {
                return false;
            }
        }

        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }

        //启用打印
        public override bool IsPrint
        {
            get
            {
                return true;
            }
        }
        #endregion

        #region 初始化方法
        /// <summary>
        /// 签收单主单显示初始化
        /// </summary>
        /// <param name="data">主单</param>
        /// <param name="item">成员</param>
        public static void InitOutNotice(ModelData data, ItemData item)
        {
            data.title = "签收单";
            switch (item.field.ToLower())
            {
                case "rowindex":
                case "sourcecode":
                case "isdisable":
                case "stay1":
                case "stay2":
                case "stay3":
                case "stay4":
                case "stay5":
                case "khlx":
                case "reviewedby":
                case "revieweddate":
                case "isreviewed":
                case "sourceoutcode":
                case "outrule":
                case "state":
                case "workerid":
                case "bumen":
                case "noticeprintnum":
                case "noticeprintstate":
                case "finishtime":
                case "submitedby":
                case "issubmited":
                case "submiteddate":
                case "sourcename":
                case "stocktype":
                    item.visible = false;
                    break;

                case "code":
                    item.title = "发货通知号";
                    item.index = 1;
                    item.disabled = true;
                    break;

                case "receiptcode":
                    item.visible = true;
                    item.title = "签收单条码";
                    item.disabled = false;
                    item.index = 2;
                    break;

                case "receiptbarcode":
                    item.visible = false;
                    item.index = 2;
                    break;

                case "outtime":
                    item.visible = true;
                    item.index = 3;
                    break;

                case "clientno":
                    item.title = "供货客户";
                    item.disabled = true;
                    item.index = 4;
                    break;
                /*
                                case "receiptcontactperson":
                                    item.visible = true;
                                    item.index = 5;
                                    break;
                */
                case "receiptclient":
                    item.title = "收货客户";
                    item.index = 6;
                    break;

                case "receiptaddress":
                    item.title = "收货地址";
                    item.index = 7;
                    break;

                case "receiptperson":
                    item.index = 8;
                    break;
                case "receiptphonecode":
                    item.visible = true;
                    item.index = 9;
                    break;

                case "remark":
                    item.index = 10;
                    break;

                case "createdby":
                    item.index = 11;
                    break;

                case "creationdate":
                    item.index = 12;
                    break;

                case "receiptstate":
                    item.visible = true;
                    item.disabled = true;
                    item.index = 13;
                    break;

                case "receiptdate":
                    item.visible = true;
                    item.disabled = true;
                    item.index = 14;
                    break;

                case "sendperson":
                    item.visible = false;
                    item.disabled = true;
                    item.index = 15;
                    break;

                case "wlgs":
                    item.visible = true;
                    item.index = 16;
                    break;

                case "waybill":
                    item.visible = true;
                    item.index = 14;
                    break;

                case "receiptprintnum":
                    item.visible = false;
                    item.index = 17;
                    break;

                case "receiptprintstate":
                    item.visible = false;
                    item.index = 18;
                    break;

                case "clientcontactperson":
                    item.visible = false;
                    item.index = 19;
                    break;

                case "clientphonecode":
                    item.visible = false;
                    item.index = 20;
                    break;
            }
        }
        /// <summary>
        /// 签收单明细显示初始化
        /// </summary>
        /// <param name="data">主单</param>
        /// <param name="item">成员</param>
        public static void InitOutNoticeMaterials(ModelData data, ItemData item)
        {
            data.disabled = true;
            data.title = "物品明细";
            switch (item.field.ToLower())
            {
                case "parentid":
                case "sourcecode":
                case "state":
                case "isdisable":
                case "code":
                case "createdby":
                case "creationdate":
                case "submitedby":
                case "issubmited":
                case "submiteddate":
                case "reviewedby":
                case "revieweddate":
                case "isreviewed":
                case "funitid":
                case "singleweight":
                case "mweight":
                    item.visible = false;
                    break;

                case "finishnum":
                    item.title = "出库数量";
                    item.disabled = true;
                    break;

                case "staynum":
                    item.title = "待出库数量";
                    item.disabled = true;
                    break;

                case "fmodel":
                    item.disabled = true;
                    break;

                case "materialcode":
                    item.title = "货物名称";
                    if (item.foreign != null)
                    {
                        item.foreign.rowdisplay.Add("FMODEL", "FMODEL");
                        item.foreign.rowdisplay.Add("CODE", "MCODE");
                    }
                    break;

                case "mcode":
                    item.title = "货物编码";
                    item.disabled = true;
                    break;

                case "receiptcode":
                    item.visible = true;
                    break;

                case "num":
                    item.title = "发货数量";
                    break;
            }
        }

        /// <summary>
        /// 初始化显示
        /// </summary>
        /// <param name="data"></param>
        /// <param name="item"></param>
        protected override void OnInitViewChildItem(Way.Contract.Data.ModelData data, Way.Contract.Data.ItemData item)
        {
            if (data.name.EndsWith("StockOutNoticeMaterials"))
            {
                InitOutNoticeMaterials(data, item);
            }
            if (data.name.EndsWith("OutNotice"))
            {
                InitOutNotice(data, item);
            }
        }

  
        #endregion


        #region 重写方法


        protected override ReadTable OutSearchData(loadItem item)
        {
            List<SQLWhere> list = new List<SQLWhere>();
            SQLWhere w = new SQLWhere();
            w.ColumnName = "PackID";
            w.Value = "0";
            SQLWhere w1 = new SQLWhere();
            w1.ColumnName = "STATE";
            w1.Symbol = "<";
            w1.Value = "3";
            list.Add(w);
            list.Add(w1);
            item.whereList = list.ToArray();
            return base.OutSearchData(item);
        }

        /// <summary>
        /// 查询按钮，并添加查询条件
        /// </summary>
        /// <param name="m"></param>
        /// <param name="where"></param>
        protected override void OnGetWhereing(IModel m, List<SQLWhere> where,loadItem item)
        {
            base.OnGetWhereing(m, where, item);
            if (m is StockOutNotice)
            {
                SQLWhere w = new SQLWhere();
                w.ColumnName = "IsSubmited";
                w.Value = "1";
                where.Add(w);
            }
        }
      
        protected override ReadTable OnForeignLoad(IModel model, loadItem item)
        {
            if (this.fdata.filedname == "WLGS")
            {
                item.rowsql = "select * from (" + item.rowsql + ") t where t.isdisable =0 and t.ftype = '1'";
            }
            return base.OnForeignLoad(model, item);
        }
    
        /// <summary>
        /// 设置按钮显示名称
        /// </summary>
        /// <param name="commands"></param>
        /// <returns></returns>
        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            ActionCommand[] coms = base.OnInitCommand(commands);
            //获取所有按钮集合
            foreach (ActionCommand ac in coms)
            {
                if (ac.command == "SubmitData" || ac.command == "UnSubmitData" || ac.command == "add" || ac.command == "remove" || ac.command == "cut")
                {
                    ac.visible = false;
                }
                
            }
            return coms;
        }

        protected override string OnPrinting(ControllerBase.PrintEvent item)
        {
            String rs = base.OnPrinting(item);
            ST_OutNotice son = item.Item as ST_OutNotice;
            son.ReceiptPrintNum = son.ReceiptPrintNum + 1;
            son.ReceiptPrintState = true;
            if (string.IsNullOrEmpty(son.ReceiptCode))
                throw new Exception("异常：签收单条码为空，不能打印！");
            EntityList<ST_OutNotice> list = new EntityList<ST_OutNotice>(this.model.GetDataAction());
            list.Add(son);
            list.Save();
            return rs;
        }
        protected override void OnEditing(ControllerBase.SaveEvent item)
        {
           // base.OnEditing(item);
        }
    }
}
        #endregion